package com.h3c.gm.service

import com.google.common.collect.Lists
import com.google.common.collect.Maps
import com.h3c.gm.dao.IBusStorageStatisticsDao
import com.h3c.gm.entity.DO.BusStorageStatisticsDO
import org.springframework.stereotype.Service

import javax.annotation.Resource

/**
 * Created by d14287 on 2017/7/14.
 */
@Service
class BusStorageStatisticsService {
    @Resource
    IBusStorageStatisticsDao busStorageStatisticsDao

    void insert(BusStorageStatisticsDO busStorageStatisticsDO) {
        busStorageStatisticsDao.insert(busStorageStatisticsDO)
    }

    void deleteByOrgkey(String orgkey) {
        busStorageStatisticsDao.deleteByOrgkey(orgkey)
    }

    Map<String, Object> getCountGroupByStatus(String orgkey) {
        def resultMap = Maps.newHashMap()

        def statusList = Lists.newArrayList()
        int activeNum, errorNum, mountNum, recycleNum

        def busStorageStatisticsDOList = busStorageStatisticsDao.getByOrgkey(orgkey)
        busStorageStatisticsDOList.each {
            busStorageStatisticsDO ->
                if ('ACTIVE'.equals(busStorageStatisticsDO.getStatus())) {
                    def activeMap = Maps.newHashMap()
                    activeMap.put('active', activeNum = busStorageStatisticsDO.getNum())
                    statusList.add(activeMap)
                }
                if ('ERROR'.equals(busStorageStatisticsDO.getStatus())) {
                    def errorMap = Maps.newHashMap()
                    errorMap.put('error', errorNum = busStorageStatisticsDO.getNum())
                    statusList.add(errorMap)
                }
                if ('MOUNT'.equals(busStorageStatisticsDO.getStatus())) {
                    def shutoffMap = Maps.newHashMap()
                    shutoffMap.put('shutoff', mountNum = busStorageStatisticsDO.getNum())
                    statusList.add(shutoffMap)
                }
                if ('RECYCLE'.equals(busStorageStatisticsDO.getStatus())) {
                    def recycleMap = Maps.newHashMap()
                    recycleMap.put('recycle', recycleNum = busStorageStatisticsDO.getNum())
                    statusList.add(recycleMap)
                }

        }
        resultMap.put('statusStatistics', statusList)
        resultMap.put('count', activeNum + errorNum + mountNum + recycleNum)
        return resultMap
    }

}
